package com.example.demo01.Service;

import com.example.demo01.Entity.SalesFunnel;
import com.example.demo01.Entity.SalesOpportunity;
import com.example.demo01.Repository.SalesFunnelRepository;
import com.example.demo01.Repository.SalesOpportunityRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SalesService {
    @Autowired
    private SalesOpportunityRepository opportunityRepository;

    @Autowired
    private SalesFunnelRepository funnelRepository;

    // 更新销售阶段
//    public void updateSalesStatus(Long opportunityId, String newStatus) {
//        // 查找销售机会
//        SalesOpportunity opportunity = opportunityRepository.findById(opportunityId)
//                .orElseThrow(() -> new IllegalArgumentException("销售机会不存在"));
//
//        // 保存旧阶段到漏斗表
//        SalesFunnel funnel = new SalesFunnel();
//        funnel.setOpportunityId(opportunityId);
//        funnel.setPreviousStatus(opportunity.getStatus());
//        funnel.setCurrentStatus(newStatus);
//        funnelRepository.save(funnel);
//
//        // 更新销售机会的当前阶段
//        opportunity.setStatus(newStatus);
//        opportunityRepository.save(opportunity);
//    }

//    public void updateSalesStatus(Long opportunityId, String newStatus) {
//        // 查找销售机会
//        SalesOpportunity opportunity = opportunityRepository.findById(opportunityId)
//                .orElseThrow(() -> new IllegalArgumentException("销售机会不存在"));
//
//        // 保存旧阶段到漏斗表
//        SalesFunnel funnel = new SalesFunnel();
//        funnel.setOpportunityId(opportunityId);
//        funnel.setPreviousStatus(opportunity.getStatus());
//        funnel.setCurrentStatus(newStatus);
//        funnelRepository.save(funnel);
//
//
//        // 更新销售机会的当前阶段
//        opportunity.setStatus(newStatus);
//        opportunityRepository.save(opportunity);
//    }
@Transactional
    public void updateSalesStatus(Long opportunityId, String newStatus) {
        // 查找销售机会
        SalesOpportunity opportunity = opportunityRepository.findById(opportunityId)
                .orElseThrow(() -> new IllegalArgumentException("销售机会不存在"));

        // 保存旧阶段到漏斗表
        SalesFunnel funnel = new SalesFunnel();
        funnel.setOpportunityId(opportunityId);
        funnel.setPreviousStatus(opportunity.getStatus()); // 原阶段
        funnel.setCurrentStatus(newStatus); // 新阶段
        funnel.setChangedAt(LocalDateTime.now()); // 设置更改时间
        funnelRepository.save(funnel);

        // 验证是否写入漏斗表
        SalesFunnel savedFunnel = funnelRepository.findById(funnel.getId())
                .orElseThrow(() -> new IllegalArgumentException("漏斗记录保存失败"));
        System.out.println("Saved funnel: " + savedFunnel);

        // 更新销售机会的当前阶段
        opportunity.setStatus(newStatus); // 更新阶段
        opportunity.setUpdatedAt(LocalDateTime.now()); // 更新修改时间
        opportunityRepository.save(opportunity);

        // 验证是否更新销售机会表
        SalesOpportunity updatedOpportunity = opportunityRepository.findById(opportunityId)
                .orElseThrow(() -> new IllegalArgumentException("销售机会更新失败"));
        System.out.println("Updated opportunity: " + updatedOpportunity);
    }



    // 获取销售阶段变更历史
//    public List<SalesFunnel> getSalesFunnelHistory(Long opportunityId) {
//        // 检查销售机会是否存在
//        SalesOpportunity opportunity = opportunityRepository.findById(opportunityId)
//                .orElseThrow(() -> new IllegalArgumentException("销售机会不存在"));
//
//        // 查询并返回历史记录
//        List<SalesFunnel> history = funnelRepository.findByOpportunityId(opportunityId);
//        if (history.isEmpty()) {
//            throw new IllegalArgumentException("未找到销售阶段历史记录");
//        }
//        // 打印日志输出
//        System.out.println("Sales Funnel History for Opportunity ID " + opportunityId + ": " + history);
//        return history;
//    }
    public List<SalesFunnel> getSalesFunnelHistory(Long opportunityId) {
        // 根据销售机会 ID 查询销售阶段历史记录
        List<SalesFunnel> history = funnelRepository.findByOpportunityId(opportunityId);
        if (history.isEmpty()) {
            throw new IllegalArgumentException("未找到销售机会ID为 " + opportunityId + " 的销售阶段历史记录");
        }
        // 打印日志输出
        System.out.println("Sales Funnel History for Opportunity ID " + opportunityId + ": " + history);
        return history;
    }



}
